package com.alibaba.citrus.cr.mn.order.center.facade.function.impl;

import com.alibaba.citrus.cr.common.enums.ordercenter.DictionaryTypeEnum;
import com.alibaba.citrus.cr.common.exception.FunctionException;
import com.alibaba.citrus.cr.mn.order.center.common.SetOrderTag;
import com.alibaba.citrus.cr.mn.order.center.common.util.AssertUtils;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.DictionaryVerifyAdapter;
import com.alibaba.citrus.cr.mn.order.center.facade.function.common.AbstractOrderBizUpdateService;
import com.alibaba.citrus.cr.mn.order.center.facade.function.common.OrderAtomUpdateService;
import com.alibaba.citrus.cr.mn.order.center.facade.function.common.OrderBizUpdateService;
import com.alibaba.citrus.cr.mn.order.center.facade.function.convert.BeanConverter;
import com.alibaba.citrus.cr.mn.order.center.facade.function.convert.OrderUpdateParamsConvertService;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.epoch.app.mnordercenter.orderbaserequest.OrderAtomUpdateRequest;
import com.epoch.app.mnordercenter.orderbaserequest.OrderAtomUpdateResponse;
import com.epoch.app.mnordercenter.orderbaserequest.OrderBizUpdateRequest;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author zhangxiaorui
 * @ClassName SalesOrderBizCreateServiceImpl
 * @description: TODO
 * @date 2023/05/16 14:04
 * @version: 1.0
 */
@Service("exchangeOrderBizUpdateService")
public class ExchangeOrderBizUpdateServiceImpl extends AbstractOrderBizUpdateService implements OrderBizUpdateService<OrderBizUpdateRequest, OrderAtomUpdateRequest, OrderAtomUpdateResponse> {
    private static final Log log = Log.getLogger(ExchangeOrderBizUpdateServiceImpl.class);

    @Resource(name = "exchangeOrderAtomUpdateService")
    private OrderAtomUpdateService exchangeOrderAtomUpdateService;
    @Resource
    private OrderUpdateParamsConvertService orderUpdateParamsConvertService;
    @Resource
    private DictionaryVerifyAdapter dictionaryVerifyAdapter;
    @Override
    public void preHandleRequestParams(OrderBizUpdateRequest bizUpdateRequest) {
        AssertUtils.isTrue(StringUtils.isBlank(bizUpdateRequest.getFromSys()), "OC-01-001-01-15-001", "来源系统编码");
        AssertUtils.isTrue(StringUtils.isBlank(bizUpdateRequest.getBusinessType()), "OC-01-001-01-15-001", "业态编码");
        AssertUtils.isTrue(StringUtils.isBlank(bizUpdateRequest.getBusinessUnitCode()), "OC-01-001-01-15-001", "业务单元编码");
        AssertUtils.isTrue(StringUtils.isBlank(bizUpdateRequest.getSalesOrderNo()), "OC-01-001-01-15-001", "换货订单号");

        AssertUtils.lengthCheck(bizUpdateRequest.getFromSys(), 50, true, "OC-01-001-01-15-004", "来源系统编码");
        AssertUtils.lengthCheck(bizUpdateRequest.getBusinessType(), 10, true, "OC-01-001-01-15-004", "业态编码");
        AssertUtils.lengthCheck(bizUpdateRequest.getBusinessUnitCode(), 50, true, "OC-01-001-01-15-004", "业务单元编码");
        AssertUtils.lengthCheck(bizUpdateRequest.getSalesOrderNo(), 50, true, "OC-01-001-01-15-004", "换货订单单号");

        dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.FROM_SYS.getCode(), bizUpdateRequest.getFromSys(),new FunctionException("OC-01-001-01-15-006", "来源系统编码"));
        dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.BUSINESS_TYPE.getCode(), bizUpdateRequest.getBusinessType(), new FunctionException("OC-01-001-01-15-006", "业态编码"));

        // 补充明细行的修改信息，数据不全会导致后面仓库和库存校验失败
        // 换货订单没有活动，没有信贷校验，没有价格，只有换货行支持库存校验
        bizUpdateRequest.setCheckCredit(Boolean.FALSE);
        if (Objects.isNull(bizUpdateRequest.getCheckStock())) {
            bizUpdateRequest.setCheckStock(Boolean.TRUE);
        }
        orderUpdateParamsConvertService.convert(bizUpdateRequest, SetOrderTag.EXCHANGE);

        super.preHandleRequestParams(bizUpdateRequest);
    }

    @Override
    public void paramsInvalidCheck(OrderBizUpdateRequest bizBatchUpdateRequest) {

    }

    @Override
    public OrderAtomUpdateResponse assembleBizBaseInfo(OrderBizUpdateRequest orderBizUpdateRequest) {
        return super.assembleBizBaseInfo(orderBizUpdateRequest);
    }

    @Override
    public OrderAtomUpdateRequest convertAtomRequest(OrderBizUpdateRequest updateRequest) {
        OrderAtomUpdateRequest atomUpdateRequest = new OrderAtomUpdateRequest();
        BeanConverter.convert(updateRequest, atomUpdateRequest);
        return atomUpdateRequest;
    }

    @Override
    public OrderAtomUpdateResponse processAtomAbility(OrderAtomUpdateRequest atomCreateRequest, OrderAtomUpdateResponse responses) {
        // 非空时异常直接返回
        if (Objects.isNull(responses)) {
            responses = exchangeOrderAtomUpdateService.update(atomCreateRequest);
            log.info("修改换货订单原子能力返回结果 Request={}, Result={}",
                    JSON.toJSONString(atomCreateRequest), JSONArray.toJSONString(responses));
        }
        return responses;
    }

    @Override
    public String getOrderTag() {
        return SetOrderTag.EXCHANGE;
    }

}
